﻿Imports System.Data.SqlClient

Public Class zmienPass
    Public wymuszonaZmianaHasla As Integer = 0

    Private Sub Zmien_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles B_zmien.Click
        Dim oldPass, pass1, pass2 As String
        Dim wynik As Boolean = False
        pass1 = hashPass(TB_pass1.Text)
        pass2 = hashPass(TB_pass2.Text)
        oldPass = hashPass(TB_oldPass.Text)

        If (pass1 = pass2) Then
            wynik = sprOldPass(oldPass)

            If (wynik = True) Then

                If (sql_zwrot_zapytania_string("SELECT wartosc FROM polityka_bezpieczenstwa WHERE parametr='polityka'") = "True") Then
                    Dim h_len_min As Integer = sql_zwrot_zapytania_string("SELECT wartosc FROM polityka_bezpieczenstwa WHERE parametr='h_len_min'")
                    Dim h_len_max As Integer = sql_zwrot_zapytania_string("SELECT wartosc FROM polityka_bezpieczenstwa WHERE parametr='h_len_max'")

                    Dim litery_male As Boolean = sql_zwrot_zapytania_string("SELECT wartosc FROM polityka_bezpieczenstwa WHERE parametr='litery_male'")
                    Dim litery_male_check As Boolean = False

                    Dim litery_duze As Boolean = sql_zwrot_zapytania_string("SELECT wartosc FROM polityka_bezpieczenstwa WHERE parametr='litery_duze'")
                    Dim litery_duze_check As Boolean = False

                    Dim cyfry As Boolean = sql_zwrot_zapytania_string("SELECT wartosc FROM polityka_bezpieczenstwa WHERE parametr='cyfry'")
                    Dim cyfry_check As Boolean = False

                    Dim znaki_spec As Boolean = sql_zwrot_zapytania_string("SELECT wartosc FROM polityka_bezpieczenstwa WHERE parametr='znaki_spec'")
                    Dim znaki_spec_check As Boolean = False

                    If (Len(TB_pass1.Text) >= h_len_min And Len(TB_pass1.Text) <= h_len_max) Then

                        For i As Integer = 0 To Len(TB_pass1.Text) - 1
                            If (Char.IsUpper(TB_pass1.Text(i))) Then
                                'MsgBox("duze: " + TB_pass1.Text(i).ToString)
                                If (litery_duze) Then
                                    litery_duze_check = True
                                End If
                            ElseIf (Char.IsNumber(TB_pass1.Text(i))) Then
                                'MsgBox("cyfra: " + TB_pass1.Text(i).ToString)
                                If (cyfry) Then
                                    cyfry_check = True
                                End If
                            ElseIf (Char.IsSymbol(TB_pass1.Text(i)) Or Char.IsPunctuation(TB_pass1.Text(i)) Or Char.IsSeparator(TB_pass1.Text(i))) Then
                                'MsgBox("symbol: " + TB_pass1.Text(i).ToString)
                                If (znaki_spec) Then
                                    znaki_spec_check = True
                                End If
                            ElseIf (Char.IsLower(TB_pass1.Text(i))) Then
                                'MsgBox("mala: " + TB_pass1.Text(i).ToString)
                                If (litery_male) Then
                                    litery_male_check = True
                                End If
                            End If
                        Next

                        'Dim komunikat As String = "Hasło nie spełnia wymagań"

                        Dim wzorzec As String = ""
                        Dim sprawdzenie As String = ""

                        wzorzec = litery_male.ToString + litery_duze.ToString + cyfry.ToString + znaki_spec.ToString
                        sprawdzenie = litery_male_check.ToString + litery_duze_check.ToString + cyfry_check.ToString + znaki_spec_check.ToString

                        'MsgBox("Polityka spełniona: " + (Chr(13) & Chr(10)) + "wzorzec" + wzorzec + (Chr(13) & Chr(10)) + "sprawdzenie" + sprawdzenie)

                        If (wzorzec = sprawdzenie) Then
                            nowePass(pass1)

                            Me.Close()
                            MsgBox("Hasło zostało pomyślnie zmienione", MsgBoxStyle.Information, "Hasło zmienione")
                            zapis_do_logu("B004", db_id_user, "")
                        Else
                            MsgBox("Wymogi odnośnie hasła nie zostały spełnione - hasło nie zmienione", MsgBoxStyle.Critical)
                            zapis_do_logu("B005", db_id_user, "")
                        End If
                    Else
                        MsgBox("Wymogi odnośnie hasła nie zostały spełnione (długość hasła) - hasło nie zmienione", MsgBoxStyle.Critical)
                        zapis_do_logu("B005", db_id_user, "")
                    End If
                Else
                    nowePass(pass1)

                    Me.Close()
                    MsgBox("Hasło zostało pomyślnie zmienione", MsgBoxStyle.Information, "Hasło zmienione")
                    zapis_do_logu("B004", db_id_user, "")
                End If

                'If (MDIParent_Panel.MenuStripUser.IsHandleCreated) Then
                '    MDIParent_Panel.MenuStripUser.Enabled = True
                'End If

            ElseIf (wynik = False) Then
                MsgBox("Błędne stare hasło", MsgBoxStyle.Critical)
                zapis_do_logu("B005", db_id_user, "")
            End If

        ElseIf (pass1 <> pass2) Then
            MsgBox("Błędnie podałeś nowe hasło. Oba ciągni znaków nowego hasła muszą być jednakowe", MsgBoxStyle.Critical, "Błąd")
            zapis_do_logu("B005", db_id_user, "")
        End If

        TB_oldPass.Text = ""
        TB_pass1.Text = ""
        TB_pass2.Text = ""
    End Sub

    Private Function sprOldPass(ByVal oldPass As String) As Boolean
        Dim querySELECT As String
        Dim SQLCCommand As New SqlCommand
        Dim reader As SqlDataReader
        Dim db_pass As String = ""

        querySELECT = "SELECT TOP 1 pass " + _
                        "FROM users, users_pass " + _
                        "WHERE users.id = users_pass.users_id " + _
                        "AND users.id=@id " + _
                        "ORDER BY id_user_pass DESC"

        SQLCCommand.Parameters.Add("@id", SqlDbType.Int).Value = DB_id_user
        SQLCCommand.CommandText = querySELECT
        SQLCCommand.CommandType = CommandType.Text

        SQLCCommand.Connection = New SqlConnection(CONN_String)

        SQLCCommand.Connection.Open()
        reader = SQLCCommand.ExecuteReader()
        reader.Read()
        db_pass = Convert.ToString(reader(0))
        SQLCCommand.Connection.Close()

        If (oldPass = db_pass) Then
            Return True
        ElseIf (oldPass <> db_pass) Then
            Return False
        End If
    End Function

    Private Sub nowePass(ByVal pass As String)
        Try
            Dim queryINSERT As String = ""
            Dim SQLCCommand As New SqlCommand

            queryINSERT = "INSERT INTO [users_pass]" + _
                               "([id_user]" + _
                               ",[pass]" + _
                               ",[data])" + _
                            "VALUES" + _
                               "(@id" + _
                               ",@pass" + _
                               ",GETDATE())"

            SQLCCommand.Parameters.Add("@id", SqlDbType.Int).Value = DB_id_user
            SQLCCommand.Parameters.Add("@pass", SqlDbType.VarChar).Value = pass

            SQLCCommand.CommandText = queryINSERT
            SQLCCommand.CommandType = CommandType.Text
            SQLCCommand.Connection = New SqlConnection(CONN_String)

            SQLCCommand.Connection.Open()
            SQLCCommand.ExecuteNonQuery()
            SQLCCommand.Connection.Close()

        Catch ex As Exception
            MsgBox(ex.ToString, MsgBoxStyle.Critical)
        End Try
    End Sub

    Private Sub B_anuluj_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles B_anuluj.Click
        Me.Close()

        If wymuszonaZmianaHasla = 1 Then
            MDI_Parent.Close()
        End If
    End Sub

    Private Sub zmienPass_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        If wymuszonaZmianaHasla = 1 Then
            MsgBox("Z powodów bezpieczeństwa wymuszona została zmiana hasła Twojego konta. W przypadku braku zmiany hasła, program zakończy swoje działanie.", MsgBoxStyle.Information)

            'If MDI_Parent.MenuStripUser.IsHandleCreated Then
            '    MDI_Parent.MenuStripUser.Enabled = False
            'End If
        End If
    End Sub

    'Private Sub TB_oldPass_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles TB_oldPass.TextChanged
    '    Dim i As Integer = Len(TB_oldPass.Text) - 1

    '    If (i >= 0) Then
    '        If (Char.IsUpper(TB_oldPass.Text(i))) Then
    '            MsgBox("duze: " + TB_oldPass.Text(i).ToString)
    '        ElseIf (Char.IsNumber(TB_oldPass.Text(i))) Then
    '            MsgBox("cyfra: " + TB_oldPass.Text(i).ToString)
    '        ElseIf (Char.IsSymbol(TB_oldPass.Text(i))) Then
    '            MsgBox("symbol: " + TB_oldPass.Text(i).ToString)
    '        ElseIf (Char.IsLower(TB_oldPass.Text(i))) Then
    '            MsgBox("mala: " + TB_oldPass.Text(i).ToString)
    '        ElseIf (Char.IsPunctuation(TB_oldPass.Text(i))) Then
    '            MsgBox("IsPunctuation: " + TB_oldPass.Text(i).ToString)
    '        ElseIf (Char.IsSeparator(TB_oldPass.Text(i))) Then
    '            MsgBox("IsSeparator: " + TB_oldPass.Text(i).ToString)
    '        ElseIf (Char.IsSurrogate(TB_oldPass.Text(i))) Then
    '            MsgBox("IsSurrogate: " + TB_oldPass.Text(i).ToString)
    '        ElseIf (Char.IsWhiteSpace(TB_oldPass.Text(i))) Then
    '            MsgBox("IsWhiteSpace: " + TB_oldPass.Text(i).ToString)
    '        Else
    '            MsgBox("???: " + TB_oldPass.Text(i).ToString)
    '        End If
    '    End If

    'End Sub
End Class